﻿using UnityEngine;
using System.Collections;

namespace RichLabelSpace
{
    public class RichSprite : RichLabelComponent
    {
        string m_atlas_name = null;
        string m_sprite_name = null;
        int m_width = 0;
        int m_height = 0;
        UISprite m_sprite = null;

        protected override void SetProperty(string[] property)
        {
            if (property[0].Equals("atlas_name"))
            {
                this.m_atlas_name = property[1];
            }
            else if (property[0].Equals("sprite_name"))
            {
                this.m_sprite_name = property[1];
            }
            else if (property[0].Equals("width"))
            {
                this.m_width = int.Parse(property[1]);
            }
            else if (property[0].Equals("height"))
            {
                this.m_height = int.Parse(property[1]);
            }

        }

        public override void Excute()
        {
            var atlas = Resources.Load<UIAtlas>(this.m_atlas_name);
            this.m_sprite.atlas = atlas;
            this.m_sprite.spriteName = this.m_sprite_name;
            this.m_sprite.pivot = UIWidget.Pivot.Left;
            this.m_sprite.MakePixelPerfect();
            if (m_width > 0) m_sprite.width = m_width;
            if (m_height > 0) m_sprite.height = m_height;
            RichLabelCtrl.ResetGameObj(this.m_ui_widget.gameObject, this.m_parent);
        }

        public RichSprite(Transform parent, RichLabelCtrl ctrl)
            : base(parent, ctrl)
        {
            var go = new GameObject();
            go.name = "RichSprite";
            go.transform.SetParent(parent);
            this.m_sprite = go.AddComponent<UISprite>();
            m_ui_widget = this.m_sprite;
        }

    }
}
